import { _detail } from '../../common/api/guest'
import { _accept, _dispatch } from '../../common/api/todos'
import { _salerlist } from '../../common/api/projects'
import { formatDate } from '../../utils/util'
const computedBehavior = require('miniprogram-computed')
const app = getApp()
Component({
  behaviors: [computedBehavior],
  data: {
    userinfo: null,
    id: '',
    pid: '',
    state: '',
    salers: [],
    salersIndex: '',
    detail: null
  },
  computed: {
    role() {
      let type = this.data.userinfo ? this.data.userinfo.AgentInfo.SalerType : ''
      return type
    },
    progress() {
      return this.data.detail ? this.data.detail.CustomerLog.map(item => {
        item.addTime = formatDate(new Date(item.AddTime), 'yyyy-MM-dd hh:mm')
        return item
      }) : []
    },
    saler () {
      let person = null
      person = this.data.salers[this.data.salersIndex]
      return person?person.Name:''
    }
  },
  methods: {
    accept() {
      let uid = wx.getStorageSync('uid')
      wx.showModal({
        title: '温馨提示',
        content: '确定接单吗？',
        success: r => {
          if (r.confirm) {
            app.loading('加载中')
            _accept(uid, this.data.id).then(res => {
              wx.hideLoading()
              wx.showModal({
                title: res.data.IsSuccess ? '温馨提示' : '对不起',
                content: res.data.Msg,
                showCancel: false,
                success: r => {
                  if (r.confirm && res.data.IsSuccess) {
                    wx.redirectTo({
                      url: '/pages/todos/list'
                    })
                  }
                }
              })
            }).catch(err => {
              wx.hideLoading()
              wx.showModal({
                title: '对不起',
                content: '请求失败，请稍后再试',
                showCancel: false
              })
            })
          }
        }
      })
    },
    dispatch () {
      if (this.data.salersIndex === '') {
        app.toast('请选择置业顾问')
        return
      }
      let uid = wx.getStorageSync('uid')
      let staffid = this.data.salers[this.data.salersIndex].ID
      wx.showModal({
        title: '温馨提示',
        content: '确定分配吗？',
        success: r => {
          if (r.confirm) {
            app.loading('加载中')
            _dispatch(uid, this.data.id, staffid, this.data.pid).then(res => {
              wx.hideLoading()
              wx.showModal({
                title: res.data.IsSuccess ? '温馨提示' : '对不起',
                content: res.data.Msg,
                showCancel: false,
                success: r => {
                  if (r.confirm && res.data.IsSuccess) {
                    wx.redirectTo({
                      url: '/pages/todos/list'
                    })
                  }
                }
              })
            }).catch(err => {
              wx.hideLoading()
              wx.showModal({
                title: '对不起',
                content: '请求失败，请稍后再试',
                showCancel: false
              })
            })
          }
        }
      })
    },
    change(e) {
      let current = e.detail.current
      let values = e.detail.values
      let checked = values[current].checked
      let tar = e.currentTarget.dataset.tar
      let tarStr = `${tar}[${current}].checked`
      this.setData({
        [tar + 'Index']: current,
        [tarStr]: checked
      })
    },
    totalQuery () {
      let uid = wx.getStorageSync('uid')
      let resolves = []
      resolves.push(Promise.resolve(_detail(uid, this.data.id)))
      if (this.data.role === '销售经理') {
        resolves.push(Promise.resolve(_salerlist(uid, this.data.pid)))
      }
      app.loading('加载中')
      Promise.all(resolves).then(res => {
        wx.hideLoading()
        let detail = res[0].data.Data
        detail.AddTime = formatDate(new Date(detail.AddTime), 'yyyy-MM-dd hh:mm')
        detail.canCall = !detail.Phone.includes('*')
        let salers = []
        if (resolves.length > 1) {
          salers = res[1].data.Data.Data.map(item => {
            item.checked = false
            return item
          })
        }
        this.setData({
          detail,
          salers
        })
      }).catch(err => {
        wx.hideLoading()
        console.log(err)
        wx.showModal({
          title: '对不起',
          content: '请求失败，请稍后再试',
          showCancel: false
        })
      })
    },
    onLoad(options) {
      this.data.id = options.orderid
      this.data.pid = options.pid
      this.data.state = options.state
      this.setData({
        state: this.data.state
      })
    },
    onReady() { },
    onShow() {
      app.launch().then(res => {
        this.setData({
          userinfo: res
        })
        this.totalQuery()
      }).catch(err => {
        console.log(err)
        if (err === '用户未注册') {
          wx.showModal({
            title: '对不起',
            content: '您还未注册',
            showCancel: false,
            success: r => {
              if (r.confirm) {
                wx.redirectTo({
                  url: '/pages/index/index'
                })
              }
            }
          })
        }
      })
    },
    onShareAppMessage() {
      return app.shareInfo
    }
  }
})